/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package practica1;

import java.util.HashMap;

/**
 *
 * @author tian
 */
public class TablaSimbolos {
  /*
   * Singleton Stuff
   */
  private static class SingletonHolder {
    public static final TablaSimbolos instance = new TablaSimbolos();
  }

  public TablaSimbolos getInstance(){
    return SingletonHolder.instance;
  }

  private HashMap<String,TS_Symbol> hm;

  private TablaSimbolos(){
    hm = new HashMap<String, TS_Symbol>();
  }

  /**
   * Stores an object at the symbols table, should have name and value
   * @param name name of the var
   * @param value value of the var
   * @param type may be null
   */
  public void storeNewVar(String name, Object value, String type){
    if (type == null)
      type = "";

    hm.put(name, new TS_Symbol(type,value));
  }

  private TS_Symbol fetchSymbol(String name){
    return hm.get(name);
  }

  public Object fetchValue(String name){
    return fetchSymbol(name).getData();
  }
  /**
   * Returns the stored type of the var
   * @param name
   * @return the type, or "" if there's no type
   */
  public String fetchType(String name){
    return fetchSymbol(name).getType();
  }

  /**
   * Primitive POJO to hold stuff about Symbols
   */
  private static class TS_Symbol{
    String type;
    Object data;

    public TS_Symbol() { }
    
    public TS_Symbol(String type, Object data) {
      this.type = type;
      this.data = data;
    }

    public Object getData() {
      return data;
    }

    public String getType() {
      return type;
    }
  }

}
